<template>
  <div>
    <van-nav-bar
      :z-index="999"
      :fixed="true"
      left-arrow
      @click-left="onClickLeft"
      :placeholder="true"
      title="实名认证"
    ></van-nav-bar>
    <!-- 实名认证 -->
    <div class="">
      <img class="logo-img" src="../../assets/images/timg.jpg" />
      <!-- 姓名和身份证的输入框 -->
      <div class="identity-input">
        <van-cell-group class="identity_item">
          <van-field v-model="name" label="姓名" placeholder="请输入姓名" />
        </van-cell-group>
        <van-cell-group class="identity_item">
          <van-field
            v-model="card"
            label="身份证"
            placeholder="请输入身份证号"
          />
        </van-cell-group>
        <div @click="attestation" class="btn-identity">认证</div>
      </div>
    </div>
  </div>
</template>

<script>
import { Toast } from "vant";
import { Authentication } from "../../assets/js/api/user.js";
export default {
  // 师傅认证
  name: "IdentityAuthentication",
  data() {
    return {
      name: "", // 姓名,
      card: "" // 身份证
    };
  },
  methods: {
    // 认证格式验证
    attestation() {
      if (this.name == "") {
        Toast.fail("姓名不能为空");
        return;
      }
      if (this.card == "") {
        Toast.fail("身份证号码不能为空");
        return;
      }
      if (
        !/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/.test(
          this.card
        )
      ) {
        Toast.fail("格式错误");
        return;
      }
 let obj = {
        name: this.name,
        sfz: this.card
      };
      // console.log(obj);
      Authentication(obj).then(res => {
        // console.log(res);
        if (res.code == 0) {
          Toast.success("认证成功");
          this.onClickLeft();
        }
        if (res.code == 502) {
          Toast.fail("认证失败");
          this.name = "";
          this.card = "";
        }
      });
 },

    onClickLeft() {
      this.$router.go(-1);
    }
  }
};
</script>

<style></style>
